<template>
  <div class="main_box" style="padding:10px;">
    <div class="add_wrap">
      <div class="tit">
        <h2 v-if="!this.addForm.itemPriceModelId">
          项 目 组 价 格 模 板 新 增
        </h2>
        <h2 v-if="this.addForm.itemPriceModelId">项 目 组 价 格 模 板 修 改</h2>
        <el-button
          type="success"
          plain
          size="small"
          icon="fa fa-mail-forward"
          @click="returnList"
        >
          返 回</el-button
        >
      </div>
      <el-form
        :model="addForm"
        ref="addForm"
        label-width="200px"
        :rules="roleRules"
      >
        <el-row>
          <el-form-item label="模板名称" prop="modelName">
            <el-input v-model="addForm.modelName" clearable></el-input>
          </el-form-item>

          <el-form-item label="模板类型" prop="modelType">
            <el-select
              v-model="addForm.modelType"
              filterable
              placeholder="请选择类型"
              :loading="loadingIns"
            >
              <el-option
                v-for="item in modelTypeList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="模板描述" prop="modelDesc">
            <el-input v-model="addForm.modelDesc" clearable></el-input>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input type="remark" v-model="addForm.remark"></el-input>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item>
            <el-button
              type="primary"
              @click="sureClick"
              :disabled="sureDisabled"
              >保 存</el-button
            >
            <el-button @click="returnList">取 消</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
import * as message from "@/constants/message";
import PriceModelApi from "@/api/pricemodel/pricemodel";
export default {
  data() {
    return {
      addForm: {
        itemPriceModelId: "",
        modelName: "",
        modelType: "",
        modelDesc: "",
        remark: ""
      },
      modelTypeList: [
        { label: "医院", value: 1 },
        { label: "医检", value: 2 }
      ],
      sureDisabled: false,
      roleRules: {
        modelName: [
          {
            required: true,
            message: "请输入模板名称",
            trigger: "blur"
          }
        ],
        modelType: [
          {
            required: true,
            message: "请选择模板类型",
            trigger: "blur"
          }
        ]
      }
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      if (this.$route.query.id) {
        this.addForm.itemPriceModelId = this.$route.query.itemPriceModelId;
        this.getData();
      }
    },
    getData() {
      let _this = this;
      PriceModelApi.itemPriceModel({
        id: _this.$route.query.id
      }).then(res => {
        // console.log(res.data)
        this.$Utils.bindingBean(this.addForm, res.data);
        if (res.data.modelType) {
          this.dataForm.modelType = parseInt(res.data.modelType);
        }
      });
    },
    sureClick() {
      let _this = this;
      _this.$refs.addForm.validate(valid => {
        if (valid) {
          if (this.addForm.itemPriceModelId) {
            //做一个基础的判断，id不为空就编辑，id为空就增加
            this.$confirm(
              message.MSG_CONFIRM_SAVE_ALTER,
              message.TITLE_PROMPT,
              {}
            ).then(() => {
              this.editData();
            });
          } else {
            this.$confirm(
              message.MSG_CONFIRM_SAVE_ADD,
              message.TITLE_PROMPT,
              {}
            ).then(() => {
              this.addData();
            });
          }
        }
      });
    },
    addData() {
      let _this = this;
      let cond = {
        modelName: _this.addForm.modelName,
        modelType: _this.addForm.modelType,
        modelDesc: _this.addForm.modelDesc,
        remark: _this.addForm.remark
      };
      PriceModelApi.addData(cond).then(data => {
        this.$notify.success({
          title: message.TITLE_SUCCESS,
          message: message.MSG_ACTION_SUCCESS
        });
        this.returnList();
      });
    },
    //修改
    editData() {
      let _this = this;
      let cond = {
        itemPriceModelId: _this.addForm.itemPriceModelId,
        modelName: _this.addForm.modelName,
        modelType: _this.addForm.modelType,
        modelDesc: _this.addForm.modelDesc,
        remark: _this.addForm.remark
      };
      PriceModelApi.updateData(cond).then(data => {
        this.$notify.success({
          title: message.TITLE_SUCCESS,
          message: message.MSG_ACTION_SUCCESS
        });
        this.returnList();
      });
    },
    returnList() {
      // this.$router.go(-1)
      this.$store.dispatch("delView", this.$route);
      this.$router.push({ path: "/items/PriceModelItemManagement" });
    }
  }
};
</script>
<style lang="scss" src="@/assets/css/views/add.scss" scope></style>
<style scoped>
.add_wrap {
  padding-top: 10px;
  padding-left: 23px;
}
</style>
